/**
 * 
 */
package com.dalonedrow.engine.ui.base.border;

import com.dalonedrow.engine.sprite.base.SpriteImageObject;
import com.dalonedrow.engine.sprite.base.SpriteImageObjectFactory;
import com.dalonedrow.engine.sprite.impl.BasicSpriteObject;
import com.dalonedrow.engine.systems.GameFont;
import com.dalonedrow.engine.systems.JOGLErrorHandler;
import com.dalonedrow.engine.systems.Render;
import com.dalonedrow.engine.ui.base.GuiComponent;
import com.dalonedrow.engine.ui.base.GuiComponentLibrary;
import com.dalonedrow.engine.ui.base.GuiTextItem;


/**
 * @author Donald
 *
 */
public class AbstractHighlightBorder extends GuiBorder {
	/** the parent component's reference id. */
	private int parentId;
	/** the id of the highlight image. */
	private int imgId;
	/**
	 * Creates a new instance of {@link AbstractHighlightBorder}.
	 * @param parent the parent component
	 * @param imgName the name of the highlight image
	 */
	AbstractHighlightBorder(final int parent, final String imgName) {
		final int four = 4;
		parentId = parent;
		try {
			imgId = 
				SpriteImageObjectFactory.getInstance().getImageRefId(imgName);
			SpriteImageObject imgArrow = 
				SpriteImageObjectFactory.getInstance().getImageByName(imgName);

			BasicSpriteObject sb = new BasicSpriteObject();
			sb.setImageRefId(imgId);
			sb.setWidth(imgArrow.getWidth());
			sb.setHeight(imgArrow.getHeight());
			super.setSprite(sb);
			super.setInsets(0, 
					imgArrow.getWidth() + four, 0, 0);
		} catch (Exception ex) {
			JOGLErrorHandler.getInstance().fatalError(ex);
		}
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void prepareForRendering() throws Exception {
		Render.getInstance().registerEntity(super.getSprite());
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public final void setPosition(final double x, final double y,
			final double z) {
		final int three = 3, four = 4;
		super.setPosition(x, y, z);
		try {
			SpriteImageObject imgArrow = 
				SpriteImageObjectFactory.getInstance().getImageById(imgId);
			GuiComponent parent = 
				GuiComponentLibrary.getInstance().getComponent(parentId);
			if (parent instanceof GuiTextItem) {
				float[] colors = 
					((GuiTextItem) parent).getColor().getRGBComponents(null);
				super.getSprite().setRed(colors[0]);
				super.getSprite().setGreen(colors[1]);
				super.getSprite().setBlue(colors[2]);
				super.getSprite().setAlpha(colors[three]);
				// position the arrow
				// go 2px to right
				double x0 = x + 2;
				double y0 = y;
				switch (((GuiTextItem) parent).getNumberOfLinesOfText()) {
				case 0:
					// do nothing - no text set
					break;
				case 1:
					// get parent font
					int font = ((GuiTextItem) parent).getFontRefId();
					int baseline = 
						GameFont.getInstance().getFontById(font).getBaseline();
					// set the y position to the baseline
					y0 += baseline;
					super.getSprite().setPosition(x0, y0, z);
					break;
				default:
					// more than one line. set highlight to middle of height
					int myHeight = imgArrow.getHeight();
					int parentHeight = 
						((GuiTextItem) parent).getPreferredSize().height;
					y0 += (parentHeight - myHeight) / 2;
					super.getSprite().setPosition(x0, y0, z);
					break;
				}
			}
		} catch (Exception ex) {
			JOGLErrorHandler.getInstance().fatalError(ex);
		}
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void setPreferredSize(final int width, final int height) {
		// TODO Auto-generated method stub
		super.setPreferredSize(width, height);
		try {
			SpriteImageObject imgArrow = 
				SpriteImageObjectFactory.getInstance().getImageById(imgId);
			super.getSprite().setWidth(imgArrow.getWidth());
			super.getSprite().setHeight(imgArrow.getHeight());
		} catch (Exception ex) {
			JOGLErrorHandler.getInstance().fatalError(ex);
		}
	}	
}
